CROSS =
CC = $(CROSS)gcc
AR = $(CROSS)ar

LIB = dicom

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
LIB_SHARED = lib$(LIB).so
LIB_STATIC = lib$(LIB).a
else ifeq ($(UNAME_S),Darwin)
LIB_SHARED = lib$(LIB).dylib
LIB_STATIC = lib$(LIB).a
endif

INCDIR = /usr/include/dicom
ifeq ($(CROSS),)
LIBDIR = /usr/lib/libdi
else
LIBDIR = `pwd`/.lib
endif

CFLAGS = -fPIC -I $(INCDIR) -D__BUILD_DICOM__
LDFLAGS = -lpthread

ifeq ($(UNAME_S),Linux)
LDFLAGS_SO = -shared
else ifeq ($(UNAME_S),Darwin)
LDFLAGS_SO = -dynamiclib -install_name '$(LIBDIR)/$(LIB_SHARED)' -current_version 1.0
endif

OBJS = queue.o string_ext.o ringbuffer.o

all: $(LIB)

%.o: %.c
	$(CC) -Wall -g -c $(CFLAGS) $< -o $@

$(LIB): $(OBJS)
	$(CC) $(LDFLAGS_SO) -o $(LIB_SHARED) $^
	$(AR) -r $(LIB_STATIC) $^

install:
	@mkdir -p $(INCDIR)
	cp `find *.h` $(INCDIR)
	@mkdir -p $(LIBDIR)
	cp $(LIB_SHARED) $(LIB_STATIC) $(LIBDIR)

clean:
	@rm -f *.o *.a *.so *.dylib
	@rm -f $(LIB)
